var M=require("ming_node");
require("./sseServer.js")

M.sseApp=M.sseServer();
const app= M.server()
app.listen(8888);

app.begin((req,res)=>{
    console.log(req.url);
    if(req.method=="OPTIONS"){ res.send({}); return}
})

app.get("/sseServer",M.sseApp)



app.set("views",".");



app.installPlugin("https://minglie.gitee.io/ming_node_plugis/src/ming_node_plgins/ming_api_mock/MingApiMockPlugin.js",
    {},
    {}
);

app.get("/runGeogebraScript",(req,res)=>{
    let geogebraScriptCode=  M.readFile("geogebraScript.js")
    M.sseApp.send( geogebraScriptCode.replace(/\r|\n/ig,""));
   res.send(M.result("ok"))
});

let geogebraCallRes=null;

app.post("/geogebraReply",(req,res)=>{
    console.log(req.params);
    if(geogebraCallRes!=null){
        geogebraCallRes.send(req.params);
        geogebraCallRes=null;
    }
    res.send(M.result("ok"))
})


app.post("/geogebraCall",(req,res)=>{
     let rpcCallParams= JSON.stringify(req.params)
      M.sseApp.send(rpcCallParams);
      geogebraCallRes=res;
})